package com.icloud.common.handler;

import com.icloud.common.InvokeResult;
import com.icloud.common.constant.InvokeStatus;
import com.icloud.common.exception.ClientException;
import com.icloud.common.exception.ServerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 异常集中处理
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 不支持当前类型
     * @param e
     * @return
     */
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public ResponseEntity<?> handle(HttpMediaTypeNotSupportedException e) {
        ClientException clientException = new ClientException(e.getMessage(), InvokeStatus.PARAMETER_ANALYSIS_ERROR);
        return ResponseEntity.ok(new InvokeResult(clientException));
    }
    /**
     * 请求方法不支持异常
     * @param e
     * @return
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseEntity<?> handle(HttpRequestMethodNotSupportedException e) {
        ClientException clientException = new ClientException(e.getMessage(), InvokeStatus.PARAMETER_ANALYSIS_ERROR);

        return ResponseEntity.ok(new InvokeResult(clientException));
    }

    /**
     * 参数绑定异常
     * @param response
     * @param ex
     * @return
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseEntity<?> handle(HttpServletResponse response, HttpMessageNotReadableException ex) {
        logger.error(ex.getMessage(),ex);
        ClientException clientException = new ClientException("参数绑定异常", InvokeStatus.PARAMETER_ANALYSIS_ERROR);
        return ResponseEntity.ok(new InvokeResult(clientException));
    }

    /**
     * 参数校验异常
     * @param methodArgumentNotValidException
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<?> handle(MethodArgumentNotValidException methodArgumentNotValidException) {
        List<FieldError> fieldErrors= methodArgumentNotValidException.getBindingResult().getFieldErrors();
        StringBuffer message = new StringBuffer();
        for(FieldError fieldError:fieldErrors){
            message.append(fieldError.getField()+":"+fieldError.getDefaultMessage()+";");
        }
        ClientException clientException = new ClientException(message.toString(), InvokeStatus.PARAMETER_ANALYSIS_ERROR);
        return ResponseEntity.ok(new InvokeResult(clientException));
    }

    @ExceptionHandler(ClientException.class)
    public ResponseEntity<?> handle(HttpServletResponse response, ClientException ex) {
        logger.error(ex.getMessage(),ex);
        ClientException clientException = new ClientException(ex.getMessage(), InvokeStatus.PARAMETER_ANALYSIS_ERROR);

        return ResponseEntity.ok(new InvokeResult(clientException));
    }

    @ExceptionHandler(ServerException.class)
    public ResponseEntity<?> handle(HttpServletResponse response, ServerException ex) {
        logger.error(ex.getMessage(),ex);
        ServerException serverException = new ServerException(ex.getMessage(), InvokeStatus.SERVER_ERORE);
        return ResponseEntity.status(500).body(new InvokeResult(serverException) );
    }


}
